﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using SingleUserMall.Common;
using SingleUserMall.Complains.Form;
using SingleUserMall.Complains.VO;
using SingleUserMall.StoreSettings.Form;
using SingleUserMall.StoreSettings.VO;
using System.Linq;
using System.Threading.Tasks;

namespace SingleUserMall.StoreSettings
{
    [AbpAuthorize]
    public class StoreSettingManageAppService : SingleUserMallAppServiceBase, IStoreSettingManageAppService
    {
        private readonly IRepository<StoreSetting, long> _storeSettingRepository;
        private readonly IRepository<AdvertSetting, long> _advertSettingRepository;
        private readonly IRepository<HomeSetting, long> _homeSettingRepository;
        public StoreSettingManageAppService(IRepository<StoreSetting, long> storeSettingRepository, IRepository<AdvertSetting, long> advertSettingRepository, IRepository<HomeSetting, long> homeSettingRepository)
        {
            _storeSettingRepository = storeSettingRepository;
            _advertSettingRepository = advertSettingRepository;
            _homeSettingRepository = homeSettingRepository;
        }
        public async Task<StoreSettingVO> GetStoreSettingAsync(SingleId<long> form)
        {
            var storeSetting = await _storeSettingRepository.FirstOrDefaultAsync(form.Id);
            return storeSetting.ToVO<StoreSetting, StoreSettingVO>();
        }

        public async Task<PagedResultDto<StoreSettingVO>> GetStoreSettingsBySearchAsync(StoreSettingSearchForm form)
        {
            var query = _storeSettingRepository.GetAll()
                .WhereIf(!string.IsNullOrEmpty(form.Keywords), x => x.Description.Contains(form.Keywords))
                .OrderByDescending(x => x.CreationTime);
            return await query.ToPagedResultDtoAsync<StoreSetting, StoreSettingVO>(form);
        }

        public async Task SaveStoreSettingAsync(StoreSettingForm form)
        {
            var storeSetting = await _storeSettingRepository.FirstOrDefaultAsync(form.Id);
            if (storeSetting != null)
            {
                storeSetting = form.MapTo(storeSetting);
                await _storeSettingRepository.UpdateAsync(storeSetting);
            }
            else
            {
                storeSetting = form.MapTo<StoreSetting>();
                await _storeSettingRepository.InsertAsync(storeSetting);
            }
        }

        public async Task RemoveAsync(SingleId<long> form)
        {
            await _storeSettingRepository.DeleteAsync(form.Id);
        }

        public async Task<AdvertSettingVO> GetAdvertSettingAsync(SingleId<long> form)
        {
            var advertSetting = await _advertSettingRepository.FirstOrDefaultAsync(form.Id);
            return advertSetting.ToVO<AdvertSetting, AdvertSettingVO>();
        }

        public async Task<PagedResultDto<AdvertSettingVO>> GetAdvertSettingsBySearchAsync(AdvertSettingSearchForm form)
        {
            var query = _advertSettingRepository.GetAll()
                .WhereIf(!string.IsNullOrEmpty(form.Keywords), x => x.Description.Contains(form.Keywords))
                .OrderByDescending(x => x.CreationTime);
            return await query.ToPagedResultDtoAsync<AdvertSetting, AdvertSettingVO>(form);
        }

        public async Task SaveAdvertSettingAsync(AdvertSettingForm form)
        {
            var advertSetting = await _advertSettingRepository.FirstOrDefaultAsync(form.Id);
            if (advertSetting != null)
            {
                advertSetting = form.MapTo(advertSetting);
                await _advertSettingRepository.UpdateAsync(advertSetting);
            }
            else
            {
                advertSetting = form.MapTo<AdvertSetting>();
                await _advertSettingRepository.InsertAsync(advertSetting);
            }
        }

        public async Task RemoveAdvertAsync(SingleId<long> form)
        {
            await _advertSettingRepository.DeleteAsync(form.Id);
        }

        public async Task<HomeSettingVO> GetHomeSettingAsync(SingleId<long> form)
        {
            var homeSetting = await _homeSettingRepository.FirstOrDefaultAsync(form.Id);
            return homeSetting.ToVO<HomeSetting, HomeSettingVO>();
        }

        public async Task<PagedResultDto<HomeSettingVO>> GetHomeSettingsBySearchAsync(HomeSettingSearchForm form)
        {
            var query = _homeSettingRepository.GetAll()
                .WhereIf(!string.IsNullOrEmpty(form.Keywords), x => x.Description.Contains(form.Keywords))
                .OrderByDescending(x => x.CreationTime);
            return await query.ToPagedResultDtoAsync<HomeSetting, HomeSettingVO>(form);
        }

        public async Task SaveHomeSettingAsync(HomeSettingForm form)
        {
            var homeSetting = await _homeSettingRepository.FirstOrDefaultAsync(form.Id);
            if (homeSetting != null)
            {
                homeSetting = form.MapTo(homeSetting);
                await _homeSettingRepository.UpdateAsync(homeSetting);
            }
            else
            {
                homeSetting = form.MapTo<HomeSetting>();
                await _homeSettingRepository.InsertAsync(homeSetting);
            }
        }

        public async Task RemoveHomeAsync(SingleId<long> form)
        {
            await _homeSettingRepository.DeleteAsync(form.Id);
        }
    }
}
